package com.wanghaixin.exception;

import com.wanghaixin.utils.R;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: 王海新
 * @Date: 2020/10/28 14:36
 * @Description: 统一处理全局异常类
 */
@ControllerAdvice
public class RExceptionHandler {



    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public R allExp(UnknownAccountException Exception){
        R r = new R();
        r.put("code",500);
        r.put("msg",Exception.getMessage());
        return r;
    }


    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  14:41  2020/10/28
     * @Version:  1.0.0
     * @Description:  异常处理：ExceptionHandler 自定义的异常
     */
    @ExceptionHandler(RException.class)//统一响应
    @ResponseBody
    public R rExp(RException rException){
        R r = new R();
        r.put("code",500);
        r.put("msg",rException.getMsg());
        return r;
    }

/*********************************************************************************************************************
 * @Author:  王海新
 * @Date:  14:42  2020/10/28
 * @Version:  1.0.0
 * @Description:  通用异常，针对所有的异常
 */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R allExp(Exception Exception){
        R r = new R();
        r.put("code",500);
        r.put("msg","系统内部异常，请联系管理员");
        return r;
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  14:46  2020/10/28
     * @Version:  1.0.0
     * @Description:  针对权限不够，跳转到指定页面
     */
    @ExceptionHandler(AuthorizationException.class)
    public String authorExce(AuthorizationException authorizationException){
        return "redirect:unauthorized.html";
    }
}
